package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TClueRemark;
import com.lzh.dlykserver.mapper.TClueRemarkMapper;
import com.lzh.dlykserver.query.ClueRemarkQuery;
import com.lzh.dlykserver.service.ClueRemarkService;
import com.lzh.dlykserver.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ClueRemarkServiceImpl implements ClueRemarkService {

    @Resource
    private TClueRemarkMapper tClueRemarkMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveaClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();
        BeanUtils.copyProperties(clueRemarkQuery, tClueRemark);

        Integer loginUserId = JWTUtils.parseUserFromJWT(clueRemarkQuery.getToken()).getId();

        tClueRemark.setCreateBy(loginUserId);
        tClueRemark.setCreateTime(new Date());

        return tClueRemarkMapper.insertSelective(tClueRemark);
    }

    @Override
    public PageInfo<TClueRemark> getClueRemarkByPage(Integer current, ClueRemarkQuery clueRemarkQuery) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        List<TClueRemark> tClueRemarkList = tClueRemarkMapper.selectClueRemarkByPage(clueRemarkQuery);

        PageInfo<TClueRemark> info = new PageInfo<>(tClueRemarkList);

        return info;
    }

    @Override
    public int editClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();
        BeanUtils.copyProperties(clueRemarkQuery, tClueRemark);

        // 编辑人的id
        Integer editById = JWTUtils.parseUserFromJWT(clueRemarkQuery.getToken()).getId();
        tClueRemark.setEditBy(editById);
        tClueRemark.setEditTime(new Date());
        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
    }

    @Override
    public TClueRemark getClueRemarkById(Integer id) {
        return tClueRemarkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delClueMark(Integer id) {
        // 逻辑删除
        TClueRemark tClueRemark = new TClueRemark();
        tClueRemark.setId(id);
        tClueRemark.setDeleted(1);
        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
    }
}
